package Modelo.Controler;

import Model.Beans.Institution;
import Model.Beans.Student;
import Model.Beans.User;
import Model.DAO.InstitutionDAO;
//import Model.Servicios.InstSrv;
//import Model.Servicios.InstSrv;
import Model.Servicios.InstSrv;
import Model.Servicios.StudentSrv;
import Model.Servicios.UserSrv;
import Model.Utilities.Constantes.StudentState;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;

public class RgstrStudentMgmtAction extends ActionSupport {

    	private Student studentBean;
        private List<String> escuelas=new ArrayList<String>();
        private UserSrv usrSrv = new UserSrv();
        private ArrayList<Student> estudiantesPend = null;
        private String institution;
        private String idStdnt;
        private List<Student> students;
        private String userIdToDelete;
        private StudentSrv studentSrv = new StudentSrv();
        
        public String listPendientes(){
            estudiantesPend = usrSrv.getStudentsPend();
            return "lista";
        }
        
        public String verDetalles(){
            String id = getIdStdnt();
            studentBean = studentSrv.getStudentById(Integer.parseInt(id));
            return "detailStudent";
        }
        
        public String aprobar(){
            String id = getIdStdnt();
            User user = new User();
            user = usrSrv.getUserByStudent(Integer.parseInt(id));
            user.setState(StudentState.APROBADO);
            studentSrv.aprobar(user);
            return "lista";
        }
	

    public String register() throws Exception {
        getStudentBean().getUser().setType(StudentState.PENDIENTE);
        StudentSrv studentBD = new StudentSrv();
        UserSrv uSrv = new UserSrv();
        uSrv.addUser(studentBean.getUser());
        Institution institute= new InstSrv().getInstitution(institution);
        getStudentBean().setInstitution(institute);
        studentBD.setStudent(studentBean);

        return SUCCESS;
    }

    @Override
    public void validate() {
        if (getStudentBean() != null) {
            if (!(getStudentBean().getFirstName().compareTo("") != 0)) {
                addFieldError("studentBean.firstName", "Faltan nombres");
            }
            if (!(getStudentBean().getLastName().compareTo("") != 0)) {
                addFieldError("studentBean.lastName", "Faltan apellidos");
            }
            if (!(getStudentBean().getEnrollment().compareTo("") != 0)) {
                addFieldError("studentBean.enrollment", "Falta matrícula");
            }
            if (!(getStudentBean().getUser().getPassword().compareTo("") != 0)) {
                addFieldError("studentBean.user.password", "Falta contraseña");
            }
            if (!(getStudentBean().getUser().getUsername().compareTo("") != 0)) {
                addFieldError("studentBean.user.username", "Falta usuario");
            }
            if ((getInstitution() == null)) {
                addFieldError("institution", "Falta escuela");
            }
        }
    }

    public Student getStudentBean() {
        return studentBean;
    }

    public void setStudentBean(Student studentBean) {
        this.studentBean = studentBean;
    }

    public List<String> getEscuelas() {
     List<Institution> escuelasT=new InstitutionDAO().getAll(Institution.class);
          for(Institution n:escuelasT){
              escuelas.add(n.getName());
          }
        return escuelas;
    }

    public void setEscuelas(List<String> escuelas) {
        this.escuelas = escuelas;
    }

    public String list() {
        students = new StudentSrv().getStudents();
        return "list";
    }
    
    public String delete() {
        new StudentSrv().deleteStudentById(getUserIdToDelete());
        return list();
    }
    
    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public String getUserIdToDelete() {
        return userIdToDelete;
    }

    public void setUserIdToDelete(String userIdToDelete) {
        this.userIdToDelete = userIdToDelete;
    }

    public String getInstitution() {
        return institution;
    }

    public void setInstitution(String institution) {
        this.institution = institution;
    }
    
        /**
     * @return the estudiantesPend
     */
    public List<Student> getEstudiantesPend() {
        return estudiantesPend;
    }

    /**
     * @return the idStdnt
     */
    public String getIdStdnt() {
        return idStdnt;
    }

    /**
     * @param idStdnt the idStdnt to set
     */
    public void setIdStdnt(String idStdnt) {
        this.idStdnt = idStdnt;
    }
    
    
}
